package com.cadre.web.controller.officer;

import com.cadre.common.utils.SecurityUtils;
import com.cadre.system.domain.officer.ITree;
import com.cadre.system.domain.officer.Tree;
import com.cadre.system.service.TreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Api(value = "单位树", tags = {"单位树"})
@RequestMapping({"/tree"})
@RestController
/* loaded from: TreeController.class */
public class TreeController {
    @Autowired
    TreeService treeSer;
    @Value("${useUnit}")
    private String useUnit;

    @GetMapping({"/init"})
    @ApiOperation("单位树")
    public List<Tree> treeInit() {
        List<Tree> TreeList = this.treeSer.treeInitByUser(SecurityUtils.getUserId().toString());
        for (Tree tree : TreeList) {
            if (tree.getPid() == null || "".equals(tree.getPid())) {
                tree.setOpen(true);
                tree.setParent(true);
                tree.setId("0");
            }
        }
        return TreeList;
    }

    @GetMapping({"/vueinit"})
    @ApiOperation("单位树")
    public List<ITree> treeVueInit() {
        List<ITree> iTrees = new ArrayList<>();
        List<Tree> TreeList = this.treeSer.treeInitByUser(SecurityUtils.getUserId().toString());
        //先获取顶级节点
        for (Tree tree : TreeList) {
            boolean flag = true;
            for (Tree tree1 : TreeList) {
                if(tree.getPid().equals(tree1.getId())){
                    flag = false;
                }
            }
            if(flag){
                ITree iTree = new ITree();
                iTree.setIcon(tree.getIcon());
                iTree.setIconClose(tree.getIconClose());
                iTree.setName(tree.getName());
                iTree.setId(tree.getId());
                iTree.setPid(tree.getPid());
                iTree.setRwflag(tree.getRwflag());
                iTree.setUnitcode(tree.getUnitcode());
                iTrees.add(iTree);
            }
        }
        iTrees = getITreeList(iTrees,TreeList);
        return iTrees;
    }

    public List<ITree> getITreeList(List<ITree> itrees,List<Tree> trees){
        if(null != itrees && itrees.size()>0){
            for(ITree iTree:itrees){
                List<ITree> children = new ArrayList<>();
                for(Tree tree: trees){
                    if(iTree.getId().equals(tree.getPid())){
                        ITree iTree1 = new ITree();
                        iTree1.setIcon(tree.getIcon());
                        iTree1.setIconClose(tree.getIconClose());
                        iTree1.setName(tree.getName());
                        iTree1.setId(tree.getId());
                        iTree1.setPid(tree.getPid());
                        iTree1.setRwflag(tree.getRwflag());
                        iTree1.setUnitcode(tree.getUnitcode());
                        children.add(iTree1);
                    }
                }
                if(null != children && children.size()>0){
                    getITreeList(children,trees);
                    iTree.setChildren(children);
                }
            }
        }
        return itrees;
    }



    @GetMapping({"/initbypdacode"})
    @ApiOperation("根据PDACODE生成单位树")
    public List<Tree> treeInitByPdacode() {
        return this.treeSer.treeInitByPdacode();
    }

    @GetMapping({"/init/user/{userId}"})
    @ApiOperation("单位树（用户）")
    public List<Tree> treeInitByUser(@PathVariable("userId") String userId) {
        List<Tree> TreeList = this.treeSer.treeInitByUser(userId);
        for (Tree tree : TreeList) {
            String parentId = tree.getPid();
            if (parentId != null) {
                boolean exists = TreeList.stream()
                                        .anyMatch(t -> t.getId().equals(parentId));
                if (!exists) {
                    tree.setPid(null);
                }
            }
        }
        return TreeList;
    }

    @GetMapping({"/initForPdacodeOrder"})
    @ApiOperation("同步unitcode到平板使用的单位树")
    public List<Tree> treeInitForPdacodeOrder() {
        return this.treeSer.treeInitForPdacodeOrder();
    }

    @GetMapping({"/init/{roleId}"})
    @ApiOperation("单位树（角色）")
    public List<Tree> treeInitByRole(@PathVariable("roleId") Long roleId) {
        List<Tree> TreeList = this.treeSer.treeInitByRole(roleId);
        for (Tree tree : TreeList) {
            if (tree.getPid() == null || "".equals(tree.getPid())) {
                tree.setOpen(true);
                tree.setParent(true);
                tree.setId("0");
            }
        }
        return TreeList;
    }
}
